package com.huagege.dao;

import java.util.List;

import org.hibernate.Session;

import com.huagege.bean.Student;
import com.huagege.util.MySessionFactory;

/**
 * 
 * @author wubobo
 *
 */
public class StudentDaoImpl implements StudentDao {

	@Override
	public void add(Student student) {
		Session session = MySessionFactory.getSession();
		session.beginTransaction();
		
		session.save(student);
		
		session.getTransaction().commit();
		session.close();
	}

	@Override
	public void remove(Student student) {
		Session session = MySessionFactory.getSession();
		session.beginTransaction();
		
		session.delete(student);
		
		session.getTransaction().commit();
		session.close();
	}

	@Override
	public void update(Student student) {
		Session session = MySessionFactory.getSession();
		session.beginTransaction();
		
		session.update(student);
		
		session.getTransaction().commit();
		session.close();
	}

	@Override
	public Student find(String id) {
		Session session = MySessionFactory.getSession();
		session.beginTransaction();
		
		Student student = (Student) session.get(Student.class, id);
		
		session.getTransaction().commit();
		session.close();
		return student;
	}

	@Override
	public List<Student> findAll() {
		Session session = MySessionFactory.getSession();
		session.beginTransaction();
		
		@SuppressWarnings("unchecked")
		List<Student> students = session.createCriteria(Student.class).list();
		
		session.getTransaction().commit();
		session.close();
		return students;
	}
	
	public int deleteAll(){
		Session session = MySessionFactory.getSession();
		session.beginTransaction();
		String hql = "delete from Student";
		int h = session.createQuery(hql).executeUpdate();
		session.getTransaction().commit();
		session.close();
		return h;
	}

}
